import { _decorator, Button, Component, director, find, instantiate, Node, Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SelectWnd')
export class SelectWnd extends Component {
    @property(Button)
    private card1: Button = null;  // 卡片1按钮

    @property(Button)
    private card2: Button = null;  // 卡片2按钮

    @property(Prefab)
    private partnerPrefab1: Prefab = null; // 伙伴1的预制体

    @property(Prefab)
    private partnerPrefab2: Prefab = null; // 伙伴2的预制体


    start() {
        // 为卡片按钮添加点击事件监听
        this.card1.node.on('click', this.onCard1Selected, this);
        this.card2.node.on('click', this.onCard2Selected, this);

    }

    update(deltaTime: number) {

    }

    // 卡片1点击事件
    private onCard1Selected() {
        console.log('卡片1被选择');
        this.onCardSelect(1);  // 调用ExpWnd的方法，选择卡片1
    }

    // 卡片2点击事件
    private onCard2Selected() {
        console.log('卡片2被选择');
        this.onCardSelect(2);  // 调用ExpWnd的方法，选择卡片2
    }

    public onCardSelect(cardType: number) {
        this.node.active = false;
        director.resume();

        let partnerPrefab = null;
        if (cardType === 1) {
            partnerPrefab = this.partnerPrefab1;
        } else if (cardType === 2) {
            partnerPrefab = this.partnerPrefab2;
        }

        if (partnerPrefab) {
            const player = find("Canvas/Player") as Node;  // 获取玩家节点
            const partner = instantiate(partnerPrefab);
            partner.setPosition(player.position);  // 设置伙伴位置为玩家位置
            player.parent.addChild(partner);  // 将伙伴添加到场景中
        }
    }
}


